package com.yoso.shop.interfaces.controller.commodity;

import com.alibaba.fastjson.JSON;
import com.yoso.base.interfaces.controller.BaseController;
import com.yoso.base.interfaces.controller.BaseResponse;
import com.yoso.base.service.IBaseService;
import com.yoso.shop.application.service.ICommodityService;
import com.yoso.shop.domain.commodity.CommodityEntity;
import com.yoso.shop.domain.commodity.CommodityEntityExample;
import com.yoso.shop.infrastructure.cache.CommodityCacheServer;
import com.yoso.shop.interfaces.vo.commodity.CommodityVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author shizejin
 * @date 2022/1/30
 * @description:
 */

@RequestMapping("commodity")
@RestController
@Api(tags = "商品信息")
@Slf4j
@CrossOrigin
public class CommodityController extends BaseController<CommodityVo, CommodityEntity> {
    @Autowired
    private ICommodityService commodityService;
    @Autowired
    private CommodityCacheServer commodityCacheServer;

    @Override
    public CommodityEntity V2E(CommodityVo data) {
        return JSON.parseObject(JSON.toJSONString(data), CommodityEntity.class);
    }

    @Override
    public CommodityVo E2V(CommodityEntity data) {
        return JSON.parseObject(JSON.toJSONString(data), CommodityVo.class);

    }

    @Override
    public IBaseService<CommodityVo, CommodityEntityExample> getService() {
        return this.commodityService;
    }

    @Override
    @PostMapping("saveOrUpdate")
    @ApiOperation("新增或修改商品信息")
    public BaseResponse saveOrUpdate(@NotNull @RequestBody CommodityVo vo) {
        return this.success(this.commodityService.saveOrUpdate(vo));
    }

    @PostMapping("saveNewCommodityInfo")
    @ApiOperation("新增商品信息")
    public BaseResponse saveNewCommodityInfo(MultipartFile[] photos, CommodityVo commodityVo) {
        return this.commodityService.saveNewCommodityInfo(commodityVo, photos);
    }


    @GetMapping("getShowMasterCommodityData")
    @ApiOperation("获取主页主分类展示数据（轮播图上）")
    public BaseResponse getShowMasterCommodityData() {
        return this.success(this.commodityService.getShowMasterCommodityData());
    }

    @Override
    public BaseResponse<CommodityVo> findById(String id) {
        CommodityVo result = this.commodityService.findDetailInfoById(id);
        return this.success(result);
    }

    @Override
    public BaseResponse<List<CommodityVo>> listAll() {
        return this.success(commodityService.listAll());
    }

    @GetMapping("getCommodityColorSize")
    @ApiOperation("获取商品颜色和大小集合")
    public BaseResponse getCommodityColorSize() {
        return this.success(this.commodityCacheServer.getColorAndSizeMap());
    }
}
